package com.dycong.common.leetcode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-08-19
 * Time: 15:34
 */
public class KuaHao {
    public boolean isValid(String s) {
        while (s.contains("()") || s.contains("[]") || s.contains("{}")) {
            if (s.contains("()")) s = s.replace("()", "");
            if (s.contains("[]")) s = s.replace("[]", "");
            if (s.contains("{}")) s = s.replace("{}", "");
        }
        return s.isEmpty();
    }

    public static void main(String[] args) {

    }

    public static boolean isValldV3(String s) {
        if (null == s) return false;
        if (s.length() == 0) return true;

        char[] stack = new char[s.length()];
        int readIndex = 0;
        int stackIndex = -1;
        char data;

        while (readIndex < s.length()) {

            data = s.charAt(readIndex);
            if (data == ' ') {
                readIndex++;
                continue;
            }

            if (data == '(' || data == '[' || data == '{') {
                stack[++stackIndex] = data;
            } else if (data == ']' || data == ')' || data == '}') {
                if (stackIndex >= 0 &&
                        ((stack[stackIndex] == '(' && data == ')')
                                || (stack[stackIndex] == '[' && data == ']') || (stack[stackIndex] == '{' && data == '}'))) {
                    stackIndex--;
                } else {
                    return false;


                }
                readIndex++;
            }

        }
        return stackIndex == -1;
    }

    public static boolean isValldV1(String s) {

        if (null == s) {
            return false;
        }

        if (s.length() == 0) {
            return true;
        }

        char[] stack = new char[s.length()];
        int stackIndex = -1;

        char data;
        int readIndex = 0;

        while (readIndex < s.length()) {
            data = s.charAt(readIndex);

            if (data == ' ') {
                readIndex++;
                continue;
            }

            if (data == '(' || data == '{' || data == '[') {
                stack[++stackIndex] = data;
            } else if (data == ')' || data == '}' || data == ']') {
                if (stackIndex >= 0
                        && ((stack[stackIndex] == '(' && data == ')')
                        || (stack[stackIndex] == '{' && data == '}')
                        || (stack[stackIndex] == '[' && data == ']'))) {
                    stackIndex--;
                } else {
                    return false;
                }
            }

            readIndex++;
        }

        return stackIndex == -1;
    }


}
